﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using System.Threading;

namespace Kiosk.Utilities
{
    /// <summary>
    /// This class takes an HTML document and generates an image from it
    /// </summary>
    public class HtmlImageGenerator : IDisposable
    {
        private WebBrowser _wb;
        private MemoryStream _memStr;
        public MemoryStream ImageStream
        {
            get { return _memStr; }
            set { _memStr = value; }
        }


        public Uri SourceUri { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }

        private bool disposed = true;
        public event GenerationCompleteEventHandler GenerationComplete;

        public HtmlImageGenerator()
        {
            _wb = new WebBrowser();
            _memStr = new MemoryStream();
        }

        public void GenerateImage()
        {
            if (_memStr != null)
            {
                _memStr.Close();
                _memStr.Dispose();
                _memStr = null;
            }

            _wb.Dispose();
            _wb = null;
            GC.Collect();
            _wb = new WebBrowser();
            _wb.ScrollBarsEnabled = false;
            _wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
            _wb.Url = this.SourceUri;
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
             //generate the image
            _wb.Width = this.Width;
            _wb.Height = this.Height;

            _memStr = new MemoryStream();

            using (System.Drawing.Bitmap _htmlBitmap = new System.Drawing.Bitmap(this.Width, this.Height,PixelFormat.Format24bppRgb))
            {
                using (System.Windows.Forms.Control c = (System.Windows.Forms.Control)_wb)
                {
                    c.DrawToBitmap(_htmlBitmap, new System.Drawing.Rectangle(0, 0, this.Width, this.Height));
                }
                // save it to a memory stream and return it as a bitmap image
                _htmlBitmap.Save(_memStr, System.Drawing.Imaging.ImageFormat.Png);
                _memStr.Position = 0;

            }
            if(GenerationComplete != null)
                GenerationComplete(this, new EventArgs());
        }

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                _wb.Dispose();
                _memStr.Dispose();
                // Note disposing has been done.
                disposed = true;

            }
        }

        ~HtmlImageGenerator()
        {
            Dispose(false);
        }

        #endregion
    }

    public delegate void GenerationCompleteEventHandler(object sender, EventArgs e);
}
